/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jobportal.company;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;
import java.text.SimpleDateFormat;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.swing.JOptionPane;
import jobportal.Dbconn;

/**
 *
 * @author project
 */
public class PostJob extends HttpServlet {

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }

    /**
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        //RequestDispatcher rd;

        Connection con;
        Statement st;
        ResultSet rs;
        String error = "";
        boolean flag = true;
        String compid = "", minmarks10 = "0", minmarks12 = "0", minmarksug = "0", minmarkspg = "0", venue, careerlevel, jobtitle, metakeys, jobdesc, desired_profile, walkindate, validuntil, gender, nationality, emp_type, jobrole, category, qual, minexp, maxexp, salary, current_sal_per, keyskill, joblocation, contname, contphone, contemail, sql;
        String dest = "", desterr = "";
        Integer clevel;
        PrintWriter out = response.getWriter();
        try {
            con = Dbconn.getConn();
            st = con.createStatement();
            HttpSession session = request.getSession();
            if (session.getAttribute("role").toString().equalsIgnoreCase("admin")) {
                desterr = "../admin/post_jobs.jsp";
                dest = "../admin/manage_jobs.jsp";
            } else if (session.getAttribute("role").toString().equalsIgnoreCase("company")) {
                desterr = "post_jobs.jsp";
                dest = "manage_jobs.jsp";
            } else {
                response.sendRedirect("../index.jsp");
            }
            compid = request.getParameter("compid");
            jobtitle = request.getParameter("jobtitle");
            metakeys = request.getParameter("metakeys");
            jobdesc = request.getParameter("jobdesc");
            desired_profile = request.getParameter("desired_profile");
            walkindate = request.getParameter("walkindate");
            java.util.Date dt = new SimpleDateFormat("dd-MMM-yy").parse(request.getParameter("walkindate"));
            SimpleDateFormat s = new SimpleDateFormat("yyyy-MM-dd");
            walkindate = s.format(dt);
            validuntil = request.getParameter("validuntil");

            dt = new SimpleDateFormat("dd-MMM-yy").parse(request.getParameter("validuntil"));
            s = new SimpleDateFormat("yyyy-MM-dd");
            validuntil = s.format(dt);
            gender = request.getParameter("gender");
            nationality = request.getParameter("nationality");
            emp_type = request.getParameter("emp_type");
            careerlevel = request.getParameter("careerlevel");
            clevel = Integer.parseInt(careerlevel);
            switch (clevel) {
                case 4:
                    minmarkspg = request.getParameter("18");
                case 3:
                    minmarksug = request.getParameter("15");
                case 2:
                    minmarks12 = request.getParameter("12");
                    minmarks10 = request.getParameter("10");
            }
            jobrole = request.getParameter("jobrole");
            category = request.getParameter("category");
            String qual1[] = request.getParameterValues("qual");
            qual = "";
            for (int j = 0; j < qual1.length; j++) {
                if (j == 0) {
                    qual = qual1[j];
                } else {
                    qual += ", " + qual1[j];
                }
            }
            //out.println("qualification:: "+qual);
            minexp = request.getParameter("minexp");
            maxexp = request.getParameter("maxexp");
            salary = request.getParameter("salary");
            current_sal_per = request.getParameter("current_sal_per");
            keyskill = request.getParameter("keyskill");
            joblocation = request.getParameter("joblocation");
            contname = request.getParameter("contname");
            contphone = request.getParameter("contphone");
            contemail = request.getParameter("contemail");
            venue = request.getParameter("venue");
            sql = "insert into jobs(jobtitle,metakeys,jobdesc,desiredprof,walkindate,validuntil,gender,nationality,emptype,careerlevel,jobrole,funarea,qualification,minexp,maxexp,salary,salarytype,keyskill,joblocation,contname,contphone,contemail,venue,jobpostdate,compid,minmarkspg,minmarksug,minmarks12,minmarks10) values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,now(),'" + compid + "',?,?,?,?)";
            PreparedStatement ps = con.prepareStatement(sql);
            ps.setString(1, jobtitle);
            ps.setString(2, metakeys);
            ps.setString(3, jobdesc);
            ps.setString(4, desired_profile);
            ps.setString(5, walkindate);
            ps.setString(6, validuntil);
            ps.setString(7, gender);
            ps.setString(8, nationality);
            ps.setString(9, emp_type);
            ps.setString(10, careerlevel);
            ps.setString(11, jobrole);
            ps.setString(12, category);
            ps.setString(13, qual);
            ps.setString(14, minexp);
            ps.setString(15, maxexp);
            ps.setString(16, salary);
            ps.setString(17, current_sal_per);
            ps.setString(18, keyskill);
            ps.setString(19, joblocation);
            ps.setString(20, contname);
            ps.setString(21, contphone);
            ps.setString(22, contemail);
            ps.setString(23, venue);
            ps.setString(24, minmarkspg);
            ps.setString(25, minmarksug);
            ps.setString(26, minmarks12);
            ps.setString(27, minmarks10);
            if (ps.executeUpdate() > 0) {
                request.setAttribute("msg", "<strong><font color=green>Job Posted Successfully</font></strong>");
          //      rd = request.getRequestDispatcher(dest);
                response.sendRedirect(dest);

            } else {
                request.setAttribute("msg", "<strong><font color=red>Error! Please enter correct details</font></strong>");
                //rd = request.getRequestDispatcher(desterr);
                //rd.forward(request, response);
                response.sendRedirect(desterr+"?msg=<strong><font color=red>Error! Please enter correct details</font></strong>");
            }

        } catch (Exception e) {
            flag = false;
            error += " Exception=" + e;
            out.println("Exception::" + e);
            request.setAttribute("msg", "<strong><font color=red>Error! "+e+"</font></strong>");
            response.sendRedirect(desterr+"?msg=<strong><font color=red>Error!"+e+"</font></strong>");
            //rd = request.getRequestDispatcher(desterr);
            //rd.forward(request, response);

        }

    }
}
